home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
SNDLIST-
/
MAIN.C
< prev
next >
Wrap
Text File
|
1988-11-10
|
14KB
|
633 lines
/*
Sound lister
"Main.c"
*/
#define GLOBALS
#include <EventMgr.h>
#include <MenuMgr.h>
#include <ResourceMgr.h>
#include <StdFilePkg.h>
#include <ToolboxUtil.h>
#include "SndList.h"
/* ----- Activate/deactivate "Play" button ----------------------------- */
void UpdatePlay(window, active)
register DocumentPeek window;
register Boolean active;
{
Point cell;
cell.v = cell.h = 0;
active = active && LGetSelect(TRUE, &cell, window->list);
SetHilite(window, iPlay, active ? 0 : 255);
OutLine(window, iPlay, active ? patCopy : notPatCopy);
}
/* ----- Identify window ----------------------------------------------- */
Boolean IsAppWindow(window)
register WindowPeek window;
{
if (!window)
return FALSE;
return window->windowKind >= 0;
}
Boolean IsDAWindow(window)
register WindowPeek window;
{
if (!window)
return FALSE;
return window->windowKind < 0;
}
/* ----- Adjust the cursor --------------------------------------------- */
void AdjustCursor(mouse, region)
Point mouse;
register RgnHandle region;
{
register DocumentPeek frontmost;
register RgnHandle arrowRgn;
register RgnHandle iBeamRgn;
Rect iBeamRect;
frontmost = (DocumentPeek)FrontWindow();
if ((!gInBackground) && (!IsDAWindow(frontmost))) {
arrowRgn = NewRgn();
iBeamRgn = NewRgn();
SetRectRgn(arrowRgn, 0x8000, 0x8000, 0x7FFE, 0x7FFE);
if (IsAppWindow(frontmost)) {
iBeamRect = (*frontmost->list)->rView;
SetPort(frontmost);
LocalToGlobal(&topLeft(iBeamRect));
LocalToGlobal(&botRight(iBeamRect));
RectRgn(iBeamRgn, &iBeamRect);
}
DiffRgn(arrowRgn, iBeamRgn, arrowRgn);
if (PtInRgn(mouse, iBeamRgn)) {
SetCursor(*GetCursor(ListCursor));
CopyRgn(iBeamRgn, region);
} else {
SetCursor(&arrow);
CopyRgn(arrowRgn, region);
}
DisposeRgn(arrowRgn);
DisposeRgn(iBeamRgn);
}
}
/* ----- Activate/Deactivate events ------------------------------------ */
void DoActivate(window)
register DocumentPeek window;
{
if (IsAppWindow(window)) {
SetPort(window);
LActivate(TRUE, window->list); /* Activate sounds list */
UpdatePlay(window, TRUE); /* Activate "Play" button */
SetHilite(window, iLevel, 0); /* Activate scroll bar */
}
}
void DoDeactivate(window)
register DocumentPeek window;
{
if (IsAppWindow(window)) {
LActivate(FALSE, window->list); /* Deactivate sounds list */
UpdatePlay(window, FALSE); /* Deactivate "Play" button */
SetHilite(window, iLevel, 255); /* Deactivate scroll bar */
}
}
/* ----- Play a sound -------------------------------------------------- */
void Play(name)
register unsigned char *name;
{
register Handle h;
if (h = Get1Resource('snd ', Extract(name))) {
HNoPurge(h);
HLock(h);
SndPlay(0L, h, FALSE);
HUnlock(h);
HPurge(h);
}
}
/* ----- Play selected sounds ------------------------------------------ */
void PlaySnds(window)
register DocumentPeek window;
{
register ListHandle list;
Point myCell; /* Current cell */
short length; /* Length of cell entry */
unsigned char name[300]; /* Name of sound resource */
short xlevel; /* To save sound level */
short ref;
short current;
GetWTitle(window, name);
if ((ref = OpenResources(name, window->volume, ¤t)) == -1)
return;
list = window->list;
HideCursor();
GetSoundVol(&xlevel);
SetSoundVol(7 - GetCheck(window, iLevel));
myCell.v = myCell.h = 0;
while(LGetSelect(TRUE, &myCell, list)) {
length = sizeof(name);
LGetCell(name, &length, myCell, list);
Play(name);
LSetSelect(FALSE, myCell, list); /* De-select the cell */
LNextCell(TRUE, TRUE, &myCell, list);
}
SetSoundVol(xlevel);
CloseResources(current, ref);
ShowCursor();
}
/* ----- Handle modeless dialog ----------------------------------------- */
void DoDialog(window, item, modifier)
register WindowPtr window;
register short item;
register short modifier;
{
if (item == iPlay) {
UpdatePlay(window, FALSE);
if (modifier & optionKey) {
EditSnds(window);
UnloadSeg(EditSnds);
} else
PlaySnds(window);
UpdatePlay(window, TRUE);
}
}
/* ----- Scroll bar filter for modeless dialog ------------------------- */
void ScrollDelay()
{
long finalTicks;
Delay(GetDblTime(), &finalTicks);
}
pascal void ScrollAction(ch, part)
register ControlHandle ch;
register short part;
{
register short value;
value = GetCtlValue(ch);
switch(part) {
case inUpButton:
ScrollDelay();
value--;
break;
case inDownButton:
ScrollDelay();
value++;
break;
case inPageUp:
value = 0;
break;
case inPageDown:
value = 7;
break;
}
SetCtlValue(ch, value);
}
void ScrollbarFilter(window, mouse)
register WindowPtr window;
Point mouse;
{
Handle ch;
register short part;
short theType;
Handle theItem;
Rect theBox;
if (IsAppWindow(window)) {
SetPort(window);
GlobalToLocal(&mouse);
part = FindControl(mouse, window, &ch);
GetDItem(window, iLevel, &theType, &theItem, &theBox);
/* The thumb is handled by the dialog manager */
if (part > 0 && ch == theItem && part != inThumb)
TrackControl(ch, mouse, ScrollAction);
}
}
/* ----- Handle mouse click in window ---------------------------------- */
void DoContentClick(window, event)
register DocumentPeek window;
register EventRecord *event;
{
Point mouse;
if (IsAppWindow(window)) {
SetPort(window);
mouse = event->where;
GlobalToLocal(&mouse);
if (LClick(mouse, event->modifiers, window->list))
DoDialog(window, iPlay, event->modifiers); /* Double click */
UpdatePlay(window, TRUE);
}
}
/* ----- Handle key down event ----------------------------------------- */
void DoKeyDown(event)
register EventRecord *event;
{
register WindowPtr window;
register unsigned char key;
if (IsAppWindow(window = FrontWindow())) {
key = event->message & charCodeMask;
if (!(event->modifiers & cmdKey)) {
if (key == '\r' || key == 0x03) /* Return, Enter */
DoDialog(window, iPlay, event->modifiers);
else
SysBeep(1);
}
}
}
/* ----- Get null event time ------------------------------------------- */
long GetSleep()
{
register long sleep;
register WindowPtr frontmost;
sleep = 0x7FFFFFFF; /* MAXLONG */
if (!gInBackground) {
frontmost = FrontWindow();
if (IsAppWindow(frontmost))
sleep = GetCaretTime();
}
return sleep;
}
/* ----- Adjust menus -------------------------------------------------- */
void AdjustMenus()
{
register WindowPtr frontmost;
register MenuHandle menu;
frontmost = FrontWindow();
menu = GetMHandle(mFile);
if (gNumDocuments < maxOpenDocuments)
EnableItem(menu, iOpen);
else
DisableItem(menu, iOpen);
if (frontmost)
EnableItem(menu, iClose);
else
DisableItem(menu, iClose);
menu = GetMHandle(mEdit);
if (IsDAWindow(frontmost)) {
EnableItem(menu, iUndo);
EnableItem(menu, iCut);
EnableItem(menu, iCopy);
EnableItem(menu, iClear);
EnableItem(menu, iPaste);
} else {
DisableItem(menu, iUndo);
DisableItem(menu, iCut);
DisableItem(menu, iCopy);
DisableItem(menu, iClear);
DisableItem(menu, iPaste);
}
}
/* ----- Close window -------------------------------------------------- */
void DoCloseWindow(window)
register DocumentPeek window;
{
if (IsDAWindow(window))
CloseDeskAcc(((WindowPeek)window)->windowKind);
else
if (IsAppWindow(window)) {
LDispose(window->list);
DisposDialog(window);
gNumDocuments--;
}
}
/* ----- Make a new sound list window ---------------------------------- */
pascal void DrawSndList(window, item) /* Called by Dialog Manager */
register DialogPtr window;
register short item;
{
short type;
Handle itemHdl;
Rect box;
GetDItem(window, item, &type, &itemHdl, &box);
LUpdate(window->visRgn, ((DocumentPeek)window)->list);
FrameRect(&box);
}
WindowPtr NewSndWindow(storage, fname, fvolume)
DocumentPeek storage;
char *fname; /* File name */
short fvolume; /* Volume reference number */
{
register WindowPtr window;
register ListHandle list;
register short i, k, length;
register Point myCell;
short current; /* Current resource file */
short ref; /* New resource file */
Handle h;
Rect bounds, array;
short id; /* Resource id of sound resource */
ResType type; /* Type of sound resource */
unsigned char info[300]; /* Name of sound resource */
unsigned char num[10];
short level;
/* Create new window */
CenterDialog('DLOG', rDialog, &Bounds);
if (!(window = GetNewDialog(rDialog, storage, -1L)))
return 0L;
SetWTitle(window, fname);
storage->volume = fvolume;
SetPort(window);
TextFont(1);
TextSize(9);
GetDItem(window, iList, &id, &h, &bounds);
SetDItem(window, iList, (short)id, &DrawSndList, &bounds);
InsetRect(&bounds, 1, 1);
/* Create the list */
if ((ref = OpenResources(fname, fvolume, ¤t)) == -1) {
CloseDialog(window);
return 0L;
}
array.top = array.left = 0;
array.bottom = Count1Resources('snd ');
array.right = 1;
CloseResources(current, ref);
if (array.bottom < 1) {
CloseDialog(window);
return 0L;
}
bounds.right -= 15;
myCell.v = 12;
myCell.h = bounds.right - bounds.left;
if (!(list = LNew(&bounds,&array,myCell,0,window,FALSE,FALSE,FALSE,TRUE))) {
CloseDialog(window);
return 0L;
}
storage->list = list;
/* Fill the list with resource names */
if ((ref = OpenResources(fname, fvolume, ¤t)) == -1) {
LDispose(list);
CloseDialog(window);
return 0L;
}
myCell.v = myCell.h = 0;
i = 1;
SetResLoad(FALSE); /* Don't load all the resource data now */
do {
if (h = Get1IndResource('snd ', i)) {
i++;
/* 0----+----1----+----2----+- */
/* 123456 1234567 xxxxxxxxxxxx */
GetResInfo(h, &id, &type, info + 14);
length = 15 + info[14];
for (k = 0; k <= 14; k++)
info[k] = 0xCA; /* Hard space */
NumToString((long)id, num);
k = num[0];
BlockMove(num + 1, info + 6 - k, (long)k);
NumToString(SizeResource(h), num);
k = num[0];
BlockMove(num + 1, info + 14 - k, (long)k);
} else
length = 0;
LSetCell(info, length, myCell, list);
} while(LNextCell(TRUE, TRUE, &myCell, list));
SetResLoad(TRUE);
CloseResources(current, ref);
GetSoundVol(&level);
SetCheck(window, iLevel, 7 - level);
LDoDraw(TRUE, list);
ShowWindow(window);
return window;
}
/* ----- Open a new file ----------------------------------------------- */
void DoNew()
{
register Ptr storage;
register WindowPtr window;
SFReply sfr;
Point where;
GetDlogOrigin(getDlgID, &where, &Bounds);
SFGetFile(where, EmptyStr, 0L, -1, EmptyStr, 0L, &sfr);
if (sfr.good) {
if (storage = NewPtr(sizeof(DocumentRecord)))
if (window = NewSndWindow(storage, sfr.fName, sfr.vRefNum))
gNumDocuments++;
else {
DisposPtr(storage);
Message(mSorry, 0);
}
}
}
/* ----- Handle menu commands ------------------------------------------ */
void DoMenuCommand(menuResult)
register long menuResult;
{
register short menuID;
register short menuItem;
unsigned char name[256];
menuID = HiWord(menuResult);
menuItem = LoWord(menuResult);
switch(menuID) {
case mApple:
switch(menuItem) {
case iAbout:
About();
UnloadSeg(About);
break;
default:
GetItem(GetMHandle(mApple), menuItem, name);
OpenDeskAcc(name);
break;
}
break;
case mFile:
switch(menuItem) {
case iOpen:
DoNew();
break;
case iClose:
DoCloseWindow(FrontWindow());
break;
case iQuit:
Terminate();
break;
}
break;
case mEdit:
SystemEdit(menuItem-1);
break;
}
HiliteMenu(0);
}
/* ----- Event handler ------------------------------------------------- */
void DoEvent(event)
register EventRecord *event;
{
#define suspendResumeMessage 1
#define resumeMask 1
#define mouseMovedMessage 0xFA
register short part;
register unsigned char key;
WindowPtr window;
short item;
/* Update filter for modeless dialog */
if (event->what == updateEvt &&
IsAppWindow(window = (WindowPtr)event->message))
UpdatePlay(window, window == FrontWindow());
/* Scrollbar filter for modeless dialog */
if (event->what == mouseDown &&
(part = FindWindow(event->where, &window)) == inContent)
ScrollbarFilter(window, event->where);
/* Modeless dialog event filter */
if (IsDialogEvent(event) && DialogSelect(event, &window, &item)) {
DoDialog(window, item, event->modifiers);
event->what = nullEvent; /* Event handled by Dialog Manager */
}
/* Now handle events */
switch(event->what) {
case mouseDown:
/*part = FindWindow(event->where, &window);*/
switch(part) {
case inMenuBar:
AdjustMenus();
DoMenuCommand(MenuSelect(event->where));
break;
case inSysWindow:
SystemClick(event, window);
break;
case inContent:
if (window != FrontWindow())
SelectWindow(window);
else
DoContentClick(window, event);
break;
case inDrag:
DragWindow(window, event->where, &Bounds);
break;
case inGoAway:
if (TrackGoAway(window, event->where))
DoCloseWindow(window);
break;
}
break;
case keyDown:
case autoKey:
key = event->message & charCodeMask;
if (event->modifiers & cmdKey) {
if (event->what == keyDown) {
AdjustMenus();
DoMenuCommand(MenuKey(key));
}
} else
DoKeyDown(event);
break;
case activateEvt:
window = (WindowPtr)event->message;
if (event->modifiers & activeFlag)
DoActivate(window);
else
DoDeactivate(window);
break;
case app4Evt:
if (((unsigned long)event->message >> 24) ==
suspendResumeMessage) {
window = FrontWindow();
if (event->message & resumeMask) {
gInBackground = FALSE;
DoActivate(window);
} else {
gInBackground = TRUE;
DoDeactivate(window);
}
}
break;
}
}
/* ----- Resume if system error ---------------------------------------- */
pascal void Crash()
{
ExitToShell();
}
/* ----- Main program -------------------------------------------------- */
void main()
{
register RgnHandle cursorRgn;
EventRecord event;
MaxApplZone();
Initialize();
UnloadSeg(Initialize);
DoNew();
cursorRgn = NewRgn();
do {
if (gHasWaitNextEvent)
WaitNextEvent(everyEvent, &event, GetSleep(), cursorRgn);
else {
SystemTask();
GetNextEvent(everyEvent, &event);
}
AdjustCursor(event.where, cursorRgn);
DoEvent(&event);
} while(TRUE);
}